package com.lagou.dao;

import com.lagou.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    @Override
    public List<User> findAll() throws IOException {

//        1、模板代码重复
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        2、statementId存在硬编码问题
        List<User> users = sqlSession.selectList("userMapper.findAll");
//        for (User user : users) {
//            System.out.println(user);
//        }
        return users;
    }
}
